package com.hoyin.viewtest;
/**
 * Author by Hoyin,
 * Email ,
 * Date on 2021/4/30.
 */
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.hoyin.javaproj.Conditions;

public class TextInputActivity extends AppCompatActivity {
	private final int PHONE_NUMBER_LENGTH = 11;
	private final int MIN_PASSWORD_LENGTH = 8;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_text_input);
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				init();
			}
		});
	}

	private void init() {
		EditText editText_loginAccount = findViewById(R.id.editText_Account);
		EditText editText_loginPassword = findViewById(R.id.editText_Password);
		Button button_loginButton = findViewById(R.id.button_LoginButton);
		CheckBox checkBox_readAgreement = findViewById(R.id.checkbox_readAgreement);
		button_loginButton.setClickable(false);
		button_loginButton.setBackgroundColor(Color.rgb(100, 100, 100));
		Conditions<Boolean> conditions = new Conditions<>();    //Conditons是自用的一个类，用于简化重复的条件判断，实现详见com.hoyin.javaproj.Conditions
		conditions.addCondition("accountCondition", false);
		conditions.addCondition("passwordCondition", false);
		conditions.addCondition("agreementCondition", false);
		editText_loginAccount.setOnFocusChangeListener((new View.OnFocusChangeListener() {
			int accountLength;
			@Override
			public void onFocusChange(View view, boolean b) {
				accountLength = String.valueOf(editText_loginAccount.getText()).length();
				conditions.addCondition("accountCondition", accountLength == PHONE_NUMBER_LENGTH);
				if (accountLength != PHONE_NUMBER_LENGTH && accountLength != 0) {
					editText_loginAccount.setTextColor(Color.RED);
				} else {
					editText_loginAccount.setTextColor(Color.BLACK);
				}
				setButtonClickable(button_loginButton, conditions.isAllTrue());
			}
		}));
		editText_loginPassword.addTextChangedListener(new TextWatcher() {
			int accountLength;
			@Override
			public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
				accountLength = String.valueOf(editText_loginAccount.getText()).length();
				conditions.addCondition("accountCondition", accountLength == PHONE_NUMBER_LENGTH);
			}

			@Override
			public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
				conditions.addCondition("passwordCondition", charSequence.length() >= MIN_PASSWORD_LENGTH);
				setButtonClickable(button_loginButton, conditions.isAllTrue());
			}

			@Override
			public void afterTextChanged(Editable editable) {

			}
		});
		checkBox_readAgreement.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
				conditions.addCondition("agreementCondition", checkBox_readAgreement.isChecked());
				setButtonClickable(button_loginButton, conditions.isAllTrue());
			}
		});
		button_loginButton.setOnClickListener(view -> {
			if (conditions.isAllTrue()) {
				Toast.makeText(this, "Login success", Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	private void setButtonClickable(Button button, boolean isClickable) {
		button.setClickable(isClickable);
		if (isClickable) {
			button.setBackgroundColor(Color.rgb(100, 100, 255));
		} else {
			button.setBackgroundColor(Color.rgb(100, 100, 100));
		}
	}
}